#! /usr/bin/env python
# encoding: utf-8

import os
import sys

APPNAME = "snakesonapayload"
VERSION = "0.1"

top = "."
out = "build"

from waflib.Task import Task
from waflib.Build import BuildContext
from waflib.Tools import ccroot
from waflib.TaskGen import after_method, feature, extension
from waflib import Options




def options(ctx):
        gr = ctx.add_option_group('snakes on a payload options')
        gr.add_option('', '--arch', dest='TARGET_ARCH', default="no-arch-set")

class testContext(BuildContext):
    cmd = "test"
    fun = "test"

def configure(conf):
    conf.load("gcc c")
    # load custom waf-tool
    conf.load("waf-tool", tooldir=".")
    
    # send .S to gcc
    from waflib.Tools.c import c_hook
    extension('.S')(c_hook)


    # add s2l to the path.
    sys.path.append( os.path.join( conf.path.abspath(), "s2l" ) )
    from archs import Arch
    
    arch = Arch.byName(Options.options.TARGET_ARCH)    
    conf.env.TARGET_ARCH_NAME = arch.NAME
    conf.env.AR = arch.toolchain.ar
    conf.env.CC = arch.toolchain.cc
    conf.env.CC_NAME = os.path.basename( arch.toolchain.cc )
    conf.env.LINK_CC = arch.toolchain.link
    conf.env.DEST_CPU = arch.cpu
    conf.env.CFLAGS = ["-fPIC", "-O0"]
    conf.env.S2LPY = os.path.join(conf.path.abspath(), "s2l/s2l.py")
    conf.env.PY2BC2H = os.path.join(conf.path.abspath(), "tinypy/py2bc2h.py")


def build(bld):
    bld.recurse( os.path.join("tests", bld.env.TARGET_ARCH_NAME) )        
    #bld.recurse( os.path.join("demo1"))
    #bld.recurse( os.path.join("payloads/tinypy"))
    
def test(bld):
    bld.env.TESTRUNNER_TOOL = os.path.join(bld.path.abspath(), "tests", bld.env.TARGET_ARCH_NAME, "testrunner.sh")
    for f in bld.bldnode.ant_glob( os.path.join("tests", bld.env.TARGET_ARCH_NAME, "*.test.bin") ):
        bld(features="testrunner", source=f, use="TESTRUNNER_TOOL")    
